<?php
namespace app\admin\model\logic;

use think\Model;

class AdminAction extends Model
{
    //逻辑层user模型初始化存储层方法
    public function __construct()
    {
        $this->action = app('app\admin\model\db\AdminAction');
        $this->auth = app('app\admin\model\db\AdminAuth');
    }
    //获取管理员权限列表
    public function get_left_menu(&$id_list)
    {
        $where['id']=['in',$id_list];
        $where['parent_id']=0;
        $where['is_show']=1;
        $rs_list=$this->action->where($where)->order(['sort'=>'asc','id'=>'desc'])->select();
        if(empty($rs_list)){
            $rs_arr['code']=0;
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_list;
        }
        return $rs_arr;
    }
    //获取父级控制器
    public function get_base_menu($son_arr)
    {
        $rs_row=$this->action->where($son_arr)->find();
        if($rs_row['parent_id']>0){
            do{
                $where['id']=$rs_row['parent_id'];
                $rs_row=$this->action->where($where)->find();
            }while($rs_row['parent_id']>0);
        }
        $rs_arr['code']=1;
        $rs_arr['data']=$rs_row['id'];
        return $rs_arr;
    }
    //获取当前控制器子菜单
    public function get_son_menu(&$base_id)
    {
        $where=['parent_id'=>$base_id,'is_show'=>1];
        $rs_list=$this->action->where($where)->order(['sort'=>'asc','id'=>'desc'])->select();
        foreach($rs_list as &$ve){
            $ve=$ve->toArray();
        }
        if(empty($rs_list)){
            $rs_arr['code']=0;
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_list;
        }
        return $rs_arr;
    }
    //获取某个菜单
    public function get_menu_row($where)
    {
        $rs_row=$this->action->where($where)->find();
        if(!empty($rs_row)){
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_row->toArray();
        }else{
            $rs_arr['code']=0;
        }
        return  $rs_arr;
    }
    //获取所有有权限的菜单列表
    public function get_action_list()
    {   
        $where['add_time']=['gt',0];
        $rs_list=$this->action->where($where)->order(['sort'=>'asc','id'=>'desc'])->select();
        foreach($rs_list as &$ve){
            $ve=$ve->toArray();
        }
        if(empty($rs_list)){
            $rs_arr['code']=0;
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_list;
        }   
        return $rs_arr;
    }
    //保存菜单
    public function action_save_add()
    {
        $post_data=input();
        $validate=validate('action');
        if($validate->scene('action_save_add')->check($post_data)){
            $rs_st=$this->action->allowField(true)->isUpdate(false)->save($post_data);
            if($rs_st!=false){
                $id=$this->action->id;
                $this->auth->base_role_add($id);//添加菜单成功自动给超级管理员一个权限
                $rs_arr['code']=1;
                $rs_arr['msg']='添加成功';
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='添加失败';
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //获取管理员权限列表
    public function generate_action_list()
    {
        $rs_act_list=$this->get_action_list();
        foreach ($rs_act_list['data'] as $r) {
            $r['id']=$r['id'];
            $r['parentid']=$r['parent_id'];
            $r['add_url']=url('add',['id'=>$r['id'],'nav'=>'添加子菜单']);
            $r['url']=url('edit',['id'=>$r['id'],'nav'=>'编辑菜单']);
            $array[] = $r;
        }
        $str = "<tr >
            <td><input name='chk_list' type='checkbox' value='\$id' /></td>
            <td style='padding-left:30px; text-align: left;'>\$spacer 
            <span data-parent_id='\$parent_id' data-id='\$id' class='tr_row'></span>
            \$name</td>
            <td > \$controller</td>
            <td > \$action</td>
            <td>\$icon</td>
            <td>\$sort</td>
            <td><a href='\$add_url' class='v_btn'>添加子菜单</a>&nbsp;&nbsp;<a href='\$url' class='v_btn'>编辑</a></td>
        </tr>";
        $rs_arr['code']=1;
        $rs_arr['data']=generate_tree($array,$str);
        return  $rs_arr;
    }
    //生成菜单下拉
    public function generate_action_option($pareng_id=0)
    {
        $rs_act_list=$this->get_action_list();
        foreach ($rs_act_list['data'] as $r) {
            $r['id']=$r['id'];
            $r['parentid']=$r['parent_id'];
            $r['selected']=$pareng_id==$r['id']?'selected':'';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer\$name</option>";
        $rs_arr['code']=1;
        $rs_arr['data']=generate_tree($array,$str);
        return  $rs_arr;
    }
    //保存菜单编辑
    public function action_save_edit()
    {
        $post_data=input();
        $validate=validate('action');
        if($validate->scene('edit')->check($post_data)){
            $rs_st=$this->action->allowField(true)->isUpdate(true)->save($post_data);
            if($rs_st!=false){
                $rs_arr['code']=1;
                $rs_arr['msg']='编辑成功';
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='编辑失败';
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //菜单删除
    public function action_del()
    {
        $post_data=input();
        $validate=validate('action');
        if($validate->scene('del')->check($post_data)){
            $where=['id'=>['in',json_decode($post_data['id_list'],true)]];
            $rs_st=$this->action->where($where)->delete();
            if($rs_st!=false){
                $rs_arr['code']=1;
                $rs_arr['msg']='删除成功'; 
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='删除失败'; 
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //获取所有菜单ID
    public function get_action_field()
    {
        return $this->action->column('id');
    }
}